package com.eco.fanliapp.utils;

import com.alibaba.fastjson.JSONObject;
import com.uAlibaba.ocean.rawsdk.ApiExecutor;
import com.uAlibaba.ocean.rawsdk.client.exception.OceanException;
import com.umeng.uapp.param.UmengUappGetAllAppDataParam;
import com.umeng.uapp.param.UmengUappGetAllAppDataResult;
import com.umeng.uapp.param.UmengUappGetTodayYesterdayDataParam;
import com.umeng.uapp.param.UmengUappGetTodayYesterdayDataResult;

/**
 * @author zhuchao
 * @create 2020-02-15 19:17
 */
public class OpenApiUtil {

	private static String appKey = "6693048";
	private static String secKey = "UyqdFpHov8no";

	/**
	 * 获取友盟App统计数据
	 * @return
	 */
	public static String getAllDate(){
		ApiExecutor apiExecutor = new ApiExecutor(appKey, secKey);
		apiExecutor.setServerHost("gateway.open.umeng.com");
		OpenApiUtil testOpenAPI = new OpenApiUtil();
		String result = testOpenAPI.umengUappGetAllAppData(apiExecutor);
		return result;
	}

	public static String getTodayYesterdayDat(int type){
		ApiExecutor apiExecutor = new ApiExecutor(appKey, secKey);
		apiExecutor.setServerHost("gateway.open.umeng.com");
		OpenApiUtil testOpenAPI = new OpenApiUtil();
		String result = testOpenAPI.umengUappGetTodayYesterdayData(apiExecutor,type);
		return result;
	}
	/**
	 * 获取昨日和今日数据统计
	 * @param apiExecutor
	 */
	public String umengUappGetTodayYesterdayData(ApiExecutor apiExecutor,int type) {
		UmengUappGetTodayYesterdayDataParam param = new UmengUappGetTodayYesterdayDataParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		if(type == 0){
			param.setAppkey("5cc6d5ea0cafb269e9000a91");//安卓
		}
		if(type == 1){
			param.setAppkey("5cc6d650570df36dd000081d");//ios
		}

		try {
			UmengUappGetTodayYesterdayDataResult result = apiExecutor.execute(param);
			System.out.println(JSONObject.toJSONString(result));
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			System.out.println("errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage());
		}
		return null;
	}

	/**
	 * 获取所有的数据
	 * @param apiExecutor
	 */
	public String umengUappGetAllAppData(ApiExecutor apiExecutor) {
		UmengUappGetAllAppDataParam param = new UmengUappGetAllAppDataParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);

		try {
			UmengUappGetAllAppDataResult result = apiExecutor.execute(param);
			System.out.println(JSONObject.toJSONString(result));
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			System.out.println("errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage());
		}
		return null;
	}

	public static void main(String[] args) {
		// 请替换apiKey和apiSecurity
//		ApiExecutor apiExecutor = new ApiExecutor("6693048", "UyqdFpHov8no");
//		apiExecutor.setServerHost("gateway.open.umeng.com");
//
//		OpenApiUtil testOpenAPI = new OpenApiUtil();
//		String str = testOpenAPI.umengUappGetTodayYesterdayData(apiExecutor, 1);
//		String str = testOpenAPI.umengUappGetAllAppData(apiExecutor);
//		String str = getAllDate();
		String str = getTodayYesterdayDat(0);
		System.out.println(str);

//		JSONArray array = (JSONArray)JSONObject.parseObject(str).get("yesterdayData");
//		JSONObject jsonObject = (JSONObject)array.get(0);
//		System.out.println(jsonObject.get("todayNewUsers"));

//		JSONObject array1 = (JSONObject)JSONObject.parseObject(str).get("yesterdayData");
//		System.out.println(array1.getString("totalUsers"));
//		Integer iosTotalUsers = (Integer)array1.get("yesterdayData");
//		System.out.println(iosTotalUsers);
		JSONObject androidYesterdayData = (JSONObject) JSONObject.parseObject(str).get("yesterdayData");
		Integer androidTotalUsers =(Integer)androidYesterdayData.get("totalUsers");
		System.out.println(androidTotalUsers);
	}



}
